package it.unicam.cs.cosy.bioshape.core.behaviours;

import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Represents a row in a {@code PafasTable} object. It contains a
 * {@code Variable} object followed by the corresponding {@code Behaviour} and a
 * list of the contained {@code Variable} objects whose availability is checked
 * by a {@code PafasTable}.
 * 
 * @author Federico Buti, Massimo Callisto De Donato
 */
public class BehaviourTuple implements Serializable {

	/** Auto-generated Serial ID */
	private static final long serialVersionUID = 4365883801162017873L;
	private Variable state;
	protected List<Variable> dependencies = new LinkedList<Variable>();
	private String name;
	private boolean visible;

	/**
	 * Generates a tuple. Called automatically by the parser.
	 * 
	 * @param name
	 *            {@code Variable} name
	 * @param state
	 *            associated {@code Behaviour}
	 * @param dependencies
	 *            list of {@code Variable} object occurring in the process
	 * @throws BehaviourException
	 *             thrown if the behaviour associated with this tuple object is
	 *             not a {@code Variable}
	 */
	public BehaviourTuple(String name, Behaviour state,
			List<Behaviour> dependencies) throws BehaviourException {
		this.name = name;
		visible = true;
		if (!(state instanceof Variable))
			throw new BehaviourException("SCParser - Processes must start with"
					+ " a process variable.");
		this.state = (Variable) state;
		for (Behaviour p : dependencies)
			this.dependencies.add((Variable) p);
	}

	/**
	 * Collects the {@code String} label of the variables objects occurring in
	 * this process.
	 * 
	 * @return The set of occurring {@code Variable}
	 */
	public Set<String> getDependencies() {
		Set<String> current = new HashSet<String>(dependencies.size());
		for (Variable pv : dependencies)
			current.add(pv.getLabel());
		return current;
	}

	/**
	 * @return The {@code Variable} object associated to this tuple
	 */
	public Variable getState() {
		return state;
	}

	protected boolean isGuarded() {
		return state.isGuarded();
	}

	@Override
	public String toString() {
		return state.toString() + " @dep<" + this.dependencies.toString()
				+ ">, " + isVisible();
	}

	/**
	 * @return the name of the {@code BehaviourTuple}
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the visibility status of the {@code BehaviourTuple}
	 */
	public boolean isVisible() {
		return visible;
	}

	/**
	 * @param state
	 *            {@code true} to make the {@code BehaviourTuple} visible,
	 *            {@code false} otherwise.
	 */
	public void setVisible(boolean state) {
		visible = state;
	}
}